﻿/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Windows Forms Controls Class Library
 * Version 2.00
 * 
 * Toolbox Control Base Class
 *      Provides the base definition for all controls in the Toolbox composite control system.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.ComponentModel;
using System.Drawing;

namespace Adaptive.Foundation.Windows.Forms
{
    /// <summary>
    /// Provides the base definition for all controls in the Toolbox composite control system.fs
    /// </summary>
    public partial class ToolboxControlBase : AdaptiveControlBase
    {
        /*--------------------------------------------------------------------------------
         * Public Event Definitions
         *------------------------------------------------------------------------------*/
        #region Public Event Definitions
        /// <summary>
        /// Occurs when one or more toolbox colors are changed.
        /// </summary>
        public event EventHandler ColorsChanged;
        /// <summary>
        /// Occurs when the item is right-clicked.
        /// </summary>
        public event EventHandler RightClick;
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Member Declarations
         *------------------------------------------------------------------------------*/
        #region Private Member Declarations

        private Color _toolboxItemForeColor = SystemColors.ControlText;
        private Color _toolboxItemBackColor = SystemColors.Control;
        private Color _toolboxItemHighlightForeColor = SystemColors.Highlight;
        private Color _toolboxItemHighlightBackColor = SystemColors.HighlightText;
        private Color _toolboxItemBorderColor = SystemColors.HotTrack;

        private int _textIndention;
        private bool _useAntiAliasing;

        #endregion

        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public ToolboxControlBase()
            : base()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Deallocates internally allocated objects.
        /// </summary>
        /// <param name="disposing">
        /// A value indicating whether the object is being disposed.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                    components.Dispose();
            }
            components = null;
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Properties
         *------------------------------------------------------------------------------*/
        #region Public Properties
        /// <summary>
        /// Gets or sets a value indicating whether the control is to use anti-aliasing
        /// on any text that is displayed.
        /// </summary>
        /// <value>
        /// <b>true</b> to anti-alias the text; otherwise, <b>false</b>.
        /// </value>
        public bool AntiAlias
        {
            get { return _useAntiAliasing; }
            set
            {
                _useAntiAliasing = value;
                Invalidate();
            }
        }
        /// <summary>
        /// Gets or sets the foreground (text) color for entries in the toolbox.
        /// </summary>
        /// <value>
        /// The foreground text color for un-highlighted items in the toolbox.
        /// </value>
        [Browsable(true),
         Description("The foreground text color for un-highlighted items in the toolbox.")]
        public Color ItemForeColor
        {
            get
            {
                return _toolboxItemForeColor;
            }
            set
            {
                if (_toolboxItemForeColor != value)
                {
                    _toolboxItemForeColor = value;
                    ResetColors();
                }
            }
        }
        /// <summary>
        /// Gets or sets the background color for entries in the toolbox.
        /// </summary>
        /// <value>
        /// The background color for un-highlighted items in the toolbox.
        /// </value>
        [Browsable(true),
         Description("The background color for un-highlighted items in the toolbox.")]
        public Color ItemBackColor
        {
            get
            {
                return _toolboxItemBackColor;
            }
            set
            {
                if (_toolboxItemBackColor != value)
                {
                    _toolboxItemBackColor = value;
                    ResetColors();
                }
            }
        }
        /// <summary>
        /// Gets or sets the highlighted foreground (text) color for entries in the toolbox.
        /// </summary>
        /// <value>
        /// The highlighted foreground text color for items in the toolbox.
        /// </value>
        [Browsable(true),
         Description("The highlighted foreground text color for items in the toolbox.")]
        public Color ItemHighlightForeColor
        {
            get
            {
                return _toolboxItemHighlightForeColor;
            }
            set
            {
                if (_toolboxItemHighlightForeColor != value)
                {
                    _toolboxItemHighlightForeColor = value;
                    ResetColors();
                }
            }
        }
        /// <summary>
        /// Gets or sets the highlighted background color for entries in the toolbox.
        /// </summary>
        /// <value>
        /// The highlighted background color for items in the toolbox.
        /// </value>
        [Browsable(true),
         Description("The highlighted background color for items in the toolbox.")]
        public Color ItemHighlightBackColor
        {
            get
            {
                return _toolboxItemHighlightBackColor;
            }
            set
            {
                if (_toolboxItemHighlightBackColor != value)
                {
                    _toolboxItemHighlightBackColor = value;
                    ResetColors();
                }
            }
        }
        /// <summary>
        /// Gets or sets the border color for entries in the toolbox when highlighted.
        /// </summary>
        /// <value>
        /// The border color for highlighted items in the toolbox.
        /// </value>
        [Browsable(true),
         Description("The border color for highlighted items in the toolbox.")]
        public Color ItemBorderColor
        {
            get
            {
                return _toolboxItemBorderColor;
            }
            set
            {
                if (_toolboxItemBorderColor != value)
                {
                    _toolboxItemBorderColor = value;
                    ResetColors();
                }
            }
        }
        /// <summary>
        /// Gets or sets the text of the control.
        /// </summary>
        /// <value>
        /// The text associated with this control.
        /// </value>
        [Browsable(true), DefaultValue(""), Description("Text to be displayed."),
         DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                if (value != base.Text)
                {
                    base.Text = value;
                    Invalidate();
                }
            }
        }
        /// <summary>
        /// Gets or sets the text indentation.
        /// </summary>
        /// <value>The text indentation value for items in the toolbox, in pixels.</value>
        [Browsable(true), DefaultValue(0),
         Description("The text indentation value for items in the toolbox, in pixels.")]
        public int TextIndentation
        {
            get { return _textIndention; }
            set
            {
                _textIndention = value;
                Invalidate();
            }
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Protected Methods / Event Methods
         *------------------------------------------------------------------------------*/
        #region Protected Methods / Event Methods
        /// <summary>
        /// Creates and assigns the event handlers for the items contained within the control.
        /// </summary>
        protected override void AssignEventHandlers()
        {
        }
        /// <summary>
        /// Removes the event handlers for the items contained within the control.
        /// </summary>
        protected override void RemoveEventHandlers()
        {
        }
        /// <summary>
        /// Initializes the content of the control before the event handlers are assigned.
        /// </summary>
        protected override void InitializeContent()
        {
        }
        /// <summary>
        /// This method is invoked in the main UI thread to perform any neccessary
        /// updates to the UI.
        /// </summary>
        /// <remarks>
        /// Calling the related <see cref="AdaptiveControlBase.UpdateControl()"/> is used to prevent illegal 
        /// cross-threaded UI update calls.
        /// </remarks>
        protected override void UpdateCurrentControlContents()
        {
        }
        /// <summary>
        /// This method is invoked in the main UI thread to perform any neccessary
        /// updates to the UI.
        /// </summary>
        /// <remarks>
        /// Calling the related <see cref="AdaptiveControlBase.UpdateControl(object[])"/> is used to prevent illegal 
        /// cross-threaded UI update calls.
        /// </remarks>
        /// <param name="data">
        /// The data supplied to the control to update the internal controls with.
        /// </param>
        protected override void UpdateCurrentControlContents(object[] data)
        {
        }
        /// <summary>
        /// Suggests the correct height value for the control.
        /// </summary>
        /// <returns>
        /// The suggested control height, in pixels.
        /// </returns>
        protected virtual int SuggestHeight()
        {
            return 0;
        }
        /// <summary>
        /// Creates a "lighter" version of the specified color value.
        /// </summary>
        /// <param name="originalValue">
        /// A red, green, or blue color value.
        /// </param>
        /// <returns>
        /// The lightened value to a maximum value of 255.
        /// </returns>
        protected int LightenValue(int originalValue)
        {
            originalValue += 50;
            if (originalValue > 255)
                originalValue = 255;

            return originalValue;
        }
        /// <summary>
        /// Creates a "lighter" version of the specified color.
        /// </summary>
        /// <param name="original">
        /// The color value to be lightened.
        /// </param>
        /// <returns>
        /// A new <see cref="Color"/> value.
        /// </returns>
        protected Color LightenColor(Color original)
        {
            Color returnValue;

            returnValue = Color.FromArgb(255, LightenValue(original.R),
                                              LightenValue(original.G),
                                              LightenValue(original.B));

            return returnValue;
        }
        /// <summary>
        /// Raises the <see cref="ColorsChanged"/> event.
        /// </summary>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected virtual void OnColorsChanged(EventArgs e)
        {
            if (ColorsChanged != null)
                ColorsChanged(this, e);
        }
        /// <summary>
        /// Raises the <see cref="E:RightClick"/> event.
        /// </summary>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected virtual void OnRightClick(EventArgs e)
        {
            if (RightClick != null)
                RightClick(this, e);
        }
        /// <summary>
        /// Forces the control to re-draw when the colors are changed.
        /// </summary>
        protected virtual void ResetColors()
        {
            Refresh();
            OnColorsChanged(EventArgs.Empty);
        }
        /// <summary>
        /// Clears the brushes used for drawing.
        /// </summary>
        protected virtual void ClearBrushes()
        {
        }
        /// <summary>
        /// Recreates the brushes used for drawing.
        /// </summary>
        protected virtual void RecreateBrushes()
        {
        }
        /// <summary>
        /// Translates the mouse down event.
        /// </summary>
        /// <param name="e">A <see cref="T:System.Windows.Forms.MouseEventArgs"/> that contains the event data.</param>
        protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
                OnRightClick(EventArgs.Empty);
            else
                base.OnMouseDown(e);
        }
        #endregion
    }
}
